package tool.analysis.series;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import tool.csv.CSVCompatible;
import tool.csv.FileWriter;

    /**
     * @author jwb09119
     * @date 2013/12/17
     * 
     * This analyser looks at the top-level use patterns for interfaces, specifically, the 
     * frequency of implementation and extension of interfaces accross an entire codebase.
     */

public class InterfaceUsersCountAnalysis extends CSVCompatible implements ISeriesAnalysis{

    private List<InterfaceUseFrequencies> records;

    public InterfaceUsersCountAnalysis(FileWriter writer) {

        super(writer);
        records = new ArrayList<>();

    }

    
    @Override
    public String getAnalysisTag () {

        return "Interface Use (Class Level)";
        
    }

    
    @Override
    public void analyseExperiments (Collection<ISeriesMember> experiments) {

        for (ISeriesMember sm : experiments) {
            
            InterfaceUseFrequencies iuf = new InterfaceUseFrequencies(sm.getSubjectName(), 
                    sm.getCountofEntitiesImplementingInterfaces(), 
                    sm.getCountOfEntitiesExtendingInterfaces(), sm.getCountOfEntities());
            
            records.add(iuf);
            
        }
        
    }

    
    /**
     * This inner class is just a 4-tuple for storing information about the  use frequencies and is
     * not intended for use outside this class.
     */
    private class InterfaceUseFrequencies {
        
        private final String name;
        private final int implementorsCount;
        private final int extendersCount;
        private final int totalEntityCount;
        
        private InterfaceUseFrequencies (String name, int imp, int ext, int count) {
            
            this.name = name;
            implementorsCount = imp;
            extendersCount = ext;
            totalEntityCount = count;
            
        }
        
    }
    
    
    /**********************
     * CSV Export Methods *
     **********************/
    @Override
    protected String getExportHeader () {

        return "Summary counts of the general use pattern for interfaces accross versions of this codebase";
        
    }

    
    @Override
    protected void writeStateToCSV (FileWriter writer) {
        
        writer.writeComment("SubjectName, ImplementsInterfaceCount, ExtendsInterfaceCount, EntityTotal");

        for (InterfaceUseFrequencies iuf : records) {
            
            writer.writeComment(iuf.name, String.valueOf(iuf.implementorsCount), 
                     String.valueOf(iuf.extendersCount), String.valueOf(iuf.totalEntityCount));
            
        }
        
    }

}
